HAL XSPI initialization and configuration functions migration

The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2:

  • Open STM32CubeMX2 XSPI configuration panel.

  • Configure the SW layer HAL.

  • Configure the XSPI main features.

  • Configure the advanced XSPI features if needed.

  • Configure the XSPI GPIO pins.

  • Configure the XSPI DMA(s) if the application requires to use the XSPI in DMA mode.

  • Configure the NVIC if the application requires the XSPI to be used in IT mode.

  • Generate the code.

HAL XSPI configuration panel

Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.

Project configuration panel

HAL XSPI main features configuration

Configure the XSPI main features.

XSPI main feature configuration

HAL XSPI advanced features configuration

Configure the advanced XSPI features if needed.

XSPI advanced feature configuration

GPIO configuration

Configure GPIO pins.

Note

The GPIO configuration is managed in XSPIM

Main features configuration

XSPI GPIO port main configuration XSPI GPIO configuration

DMA configuration

Configure the DMA for data reception as needed.

XSPI DMA configuration

NVIC configuration

Configure the NVIC in case of an interrupt.

XSPI NVIC configuration

Generated XSPI initialization and de-initialization sequences

The XSPI initialization sequence is generated in the mx_xspi1_init function in mx_xspi1.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.

Related concepts:

STM32CubeMX2 Generated : XSPI initialization and de-initialization sequences

Topic

HAL1 code snippet

HAL2 code snippet

Initialization and configuration sequence

static void MX_XSPI1_Init(void)
{

  XSPIM_CfgTypeDef sXspiManagerCfg = {0};













  /* XSPI1 parameter configuration*/
  hxspi1.Instance = XSPI1;
  hxspi1.Init.FifoThresholdByte = 1;
  hxspi1.Init.MemoryMode = HAL_XSPI_SINGLE_MEM;
  hxspi1.Init.MemoryType = HAL_XSPI_MEMTYPE_MICRON;
  hxspi1.Init.MemorySize = HAL_XSPI_SIZE_16B;
  hxspi1.Init.ChipSelectHighTimeCycle = 1;
  hxspi1.Init.FreeRunningClock = HAL_XSPI_FREERUNCLK_DISABLE;
  hxspi1.Init.ClockMode = HAL_XSPI_CLOCK_MODE_0;
  hxspi1.Init.WrapSize = HAL_XSPI_WRAP_NOT_SUPPORTED;
  hxspi1.Init.ClockPrescaler = 0x1U;
  hxspi1.Init.SampleShifting = HAL_XSPI_SAMPLE_SHIFT_NONE;
  hxspi1.Init.DelayHoldQuarterCycle = HAL_XSPI_DHQC_DISABLE;
  hxspi1.Init.ChipSelectBoundary = HAL_XSPI_BONDARYOF_NONE;
  hxspi1.Init.MaxTran = 0;
  hxspi1.Init.Refresh = 0;
  hxspi1.Init.MemorySelect = HAL_XSPI_CSSEL_NCS1;
  if (HAL_XSPI_Init(&hxspi1) != HAL_OK)
  {
    Error_Handler();
  }
  sXspiManagerCfg.nCSOverride = HAL_XSPI_CSSEL_OVR_NCS1;
  sXspiManagerCfg.IOPort = HAL_XSPIM_IOPORT_1;
  if (HAL_XSPIM_Config(&hxspi1, &sXspiManagerCfg, HAL_XSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
  {
    Error_Handler();
  }

}

































void HAL_XSPI_MspInit(XSPI_HandleTypeDef* hxspi)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  if(hxspi->Instance==XSPI1)
  {
  /** Initializes the peripherals clock */
    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_XSPI1;
    PeriphClkInit.Xspi1ClockSelection = RCC_XSPI1CLKSOURCE_PLL2S;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
    {
      Error_Handler();
    }

    /* Peripheral clock enable */
    __HAL_RCC_XSPIM_CLK_ENABLE();
    __HAL_RCC_XSPI1_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_GPIOB_CLK_ENABLE();
    __HAL_RCC_GPIOC_CLK_ENABLE();

    GPIO_InitStruct.Pin = HAL_GPIO_PIN_3 | HAL_GPIO_PIN_1 | HAL_GPIO_PIN_2 | HAL_GPIO_PIN_7 | HAL_GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = HAL_GPIO_AF_10;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);








    GPIO_InitStruct.Pin = HAL_GPIO_PIN_12 | HAL_GPIO_PIN_1 | HAL_GPIO_PIN_0;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = HAL_GPIO_AF_10;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);








    GPIO_InitStruct.Pin = HAL_GPIO_PIN_1 | HAL_GPIO_PIN_2 | HAL_GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = HAL_GPIO_AF_10;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);





    GPIO_InitStruct.Pin = HAL_GPIO_PIN_0;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = HAL_GPIO_AF_3;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  }
}
hal_xspi_handle_t *mx_octospi1_hal_init(void)
{
  hal_xspi_config_t xspi_config;
  hal_xspi_memory_config_t memory_config;
  hal_xspi_timing_config_t timing_config;
  uint32_t p_max_clock_phase;

  if (HAL_XSPI_Init(&hOCTOSPI1, HAL_OCTOSPI1) != HAL_OK)
  {
    return NULL;
  }

  HAL_RCC_OCTOSPI1_EnableClock();
  HAL_RCC_GPIOP_EnableClock();
  HAL_RCC_GPIOO_EnableClock();
  if (HAL_RCC_OCTOSPI_SetKernelClkSource(HAL_RCC_OCTOSPI_CLK_SRC_SYSCLK) != HAL_OK)
  {
    return NULL;
  }
  /* Main peripheral configuration */
  memory_config.mode = HAL_XSPI_MEMORY_SINGLE;
  memory_config.type = HAL_XSPI_MEMORY_TYPE_MICRON;
  memory_config.size_bit = HAL_XSPI_MEMORY_SIZE_16BIT;
  memory_config.wrap_size_byte = HAL_XSPI_WRAP_NOT_SUPPORTED;
  memory_config.clock_polarity = HAL_XSPI_CLOCK_LOW;
  memory_config.cs_boundary = HAL_XSPI_CS_BOUNDARY_NONE;
  timing_config.clk_prescaler = 0x1U;
  timing_config.shift = HAL_XSPI_SAMPLE_SHIFT_NONE;
  timing_config.hold = HAL_XSPI_DELAY_HOLD_NONE;
  timing_config.cs_high_time_cycle = 0x1U;
  timing_config.cs_release_time_cycle = 0x0U;
  timing_config.cs_refresh_time_cycle = 0x0U;
  timing_config.dlyb_state = HAL_XSPI_DLYB_ON;

  xspi_config.memory = memory_config;
  xspi_config.timing = timing_config;

  if (HAL_XSPI_SetConfig(&hOCTOSPI1, &xspi_config) != HAL_OK)
  {
    return NULL;
  }

  /* Delay block configuration */
  if (HAL_XSPI_DLYB_Enable(&hOCTOSPI1) != HAL_OK)
  {
    return NULL;
  }
  if (HAL_XSPI_DLYB_CalculateMaxClockPhase(&hOCTOSPI1, &p_max_clock_phase) != HAL_OK)
  {
    return NULL;
  }

  if (HAL_XSPI_DLYB_SetConfigDelay(&hOCTOSPI1, p_max_clock_phase) != HAL_OK)
  {
    return NULL;
  }

  return &hOCTOSPI1;
}


system_status_t mx_octospim_hal_init(void)
{
  hal_xspi_xspim_config_t xspim_config;

  HAL_RCC_OCTOSPIM_EnableClock();

  xspim_config.port1.clk = HAL_XSPI_XSPIM_XSPI1;
  xspim_config.port1.dqs = HAL_XSPI_XSPIM_XSPI1;
  xspim_config.port1.ncs = HAL_XSPI_XSPIM_XSPI1;
  xspim_config.port1.io_low = HAL_XSPI_XSPIM_XSPI1_IO_LOW;
  xspim_config.port1.io_high = HAL_XSPI_XSPIM_XSPI1_IO_HIGH;

  xspim_config.port2.clk = HAL_XSPI_XSPIM_NONE;
  xspim_config.port2.dqs = HAL_XSPI_XSPIM_NONE;
  xspim_config.port2.ncs = HAL_XSPI_XSPIM_NONE;
  xspim_config.port2.io_low = HAL_XSPI_XSPIM_IO_NONE;
  xspim_config.port2.io_high = HAL_XSPI_XSPIM_IO_NONE;

  xspim_config.req_ack_cycle = 0x0UL;

  if (HAL_XSPI_XSPIM_SetConfig(&xspim_config) != HAL_OK)
  {
    return SYSTEM_PERIPHERAL_ERROR;
  }


  HAL_RCC_GPIOA_EnableClock();
  HAL_RCC_GPIOB_EnableClock();
  HAL_RCC_GPIOC_EnableClock();
  hal_gpio_config_t  gpio_config;

  /**
    OCTOSPIM GPIO Configuration
    PA3   --------->  OCTOSPIM_P1_CLK
    PA1   --------->  OCTOSPIM_P1_DQS
    PA2   --------->  OCTOSPIM_P1_NCS
    PA7   --------->  OCTOSPIM_P1_IO2
    PA6   --------->  OCTOSPIM_P1_IO3
    **/
  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_10;
  HAL_GPIO_Init(HAL_GPIOA, HAL_GPIO_PIN_3 | HAL_GPIO_PIN_1 | HAL_GPIO_PIN_2 | HAL_GPIO_PIN_7 | HAL_GPIO_PIN_6, &gpio_config);


  /**
    OCTOSPIM GPIO Configuration
    PB12  --------->  OCTOSPIM_P1_NCLK
    PB1   --------->  OCTOSPIM_P1_IO0
    PB0   --------->  OCTOSPIM_P1_IO1
    **/
  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_10;
  HAL_GPIO_Init(HAL_GPIOB, HAL_GPIO_PIN_12 | HAL_GPIO_PIN_1 | HAL_GPIO_PIN_0, &gpio_config);


  /**
    OCTOSPIM GPIO Configuration
    PC1   --------->  OCTOSPIM_P1_IO4
    PC2   --------->  OCTOSPIM_P1_IO5
    PC3   --------->  OCTOSPIM_P1_IO6
    **/
  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_10;
  HAL_GPIO_Init(HAL_GPIOC, HAL_GPIO_PIN_1 | HAL_GPIO_PIN_2 | HAL_GPIO_PIN_3, &gpio_config);


  /**
    OCTOSPIM GPIO Configuration
    PC0   --------->  OCTOSPIM_P1_IO7
    **/
  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_3;
  HAL_GPIO_Init(HAL_GPIOC, HAL_GPIO_PIN_0, &gpio_config);

  return SYSTEM_OK;
}

Deinitialization sequence

static void MX_XSPI1_DeInit(void)
{
  HAL_XSPI_DeInit(&hxspi);
}




void HAL_XSPI_MspDeInit(XSPI_HandleTypeDef* hxspi)
{
  if(hxspi->Instance==XSPI1)
  {
    /* Peripheral clock disable */
    __HAL_RCC_XSPIM_CLK_DISABLE();
    __HAL_RCC_XSPI1_CLK_DISABLE();

    HAL_GPIO_DeInit(GPIOA, HAL_GPIO_PIN_3 | HAL_GPIO_PIN_1 | HAL_GPIO_PIN_2 | HAL_GPIO_PIN_7 | HAL_GPIO_PIN_6);
    HAL_GPIO_DeInit(GPIOB, HAL_GPIO_PIN_12 | HAL_GPIO_PIN_1 | HAL_GPIO_PIN_0);
    HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0|HAL_GPIO_PIN_1 | HAL_GPIO_PIN_2 | HAL_GPIO_PIN_3);
  }
}
void mx_octospi1_hal_deinit(void)
{
  (void)HAL_XSPI_DeInit(&hOCTOSPI1);
  HAL_RCC_OCTOSPI1_Reset();
  HAL_RCC_OCTOSPI1_DisableClock();
}

void mx_octospim_hal_deinit(void)
{
  HAL_RCC_OCTOSPIM_Reset();
  HAL_RCC_OCTOSPIM_DisableClock();
}